package cqie.bigdata;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


public class TestService {

    public static void testService() {
        try {

            // 具体服务的调用地址int timeout = 500;
            // 服务调用延时String clientId = "YOUR_CLIENT_ID";
            // 替换为用户的client_idString timestamp = String.valueOf(Instant.now().toEpochMilli());
            // String nonce = UUID.randomUUID().toString();
            // String secret = "YOUR_CLIENT_SECRET";

            String secret = "";
            String clientId = "";
            String timestamp = String.valueOf(Instant.now().toEpochMilli());
            String nonce = UUID.randomUUID().toString();

            // 替换为用户的client_secretString stringToSign = clientId + timestramp + nonce;
            String stringToSign = clientId + timestamp + nonce;

            String serviceUrl = "http://data.chengdu.gov.cn/gateway/api/1/ssthjj/gjcsmccxqg169csrpm";

            Mac hmacSha256 = Mac.getInstance("HmacSHA256");
            byte[] keyBytes = secret.getBytes("UTF-8");
            hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, "HmacSHA256"));
            String sign = Base64.encodeBase64String(hmacSha256.doFinal(stringToSign.getBytes("UTF-8")));

            HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties();
            CloseableHttpClient httpClient = httpClientBuilder.build();

//            HttpPost requestMethod = new HttpPost();
            // 根据服务的具体请求方法构造合适的请求方法对象，此处以POST方法为例说明requestMethod.setURI(serviceUrl);
            HttpPost requestMethod = new HttpPost(serviceUrl);
            List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
            nvps.add(new BasicNameValuePair("cityname", "重庆市"));
            requestMethod.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));

            RequestConfig config = RequestConfig.custom().setConnectTimeout(3000).build();
            requestMethod.setConfig(config);
            requestMethod.addHeader("X-Client-Id", clientId);
            requestMethod.addHeader("X-Timestamp", timestamp);
            requestMethod.addHeader("X-Nonce", nonce);
            requestMethod.addHeader("X-Signature", sign);
            CloseableHttpResponse response = httpClient.execute(requestMethod);

            // 取响应的结果
            int statusCode = response.getStatusLine().getStatusCode();
            // 打印响应结果
            if (statusCode == HttpStatus.SC_OK) {
                String resp = EntityUtils.toString(response.getEntity(), "utf-8");
                System.out.println("status:" + statusCode);
                System.out.println("result:" + resp);
            }

        } catch (NoSuchAlgorithmException e) {
        } catch (UnsupportedEncodingException e) {
        } catch (InvalidKeyException e) {
        } catch (Exception e) {
        }

    }
}
